<template>
	<view class="container container-bg">
		<u-form :model="form" ref="uForm" label-width="auto">
			<u-form-item label-width="120" required="true" label="项目名称" prop="project_name">
				<u-input v-model="form.project_name" placeholder="请输入项目名称" />
			</u-form-item>
			<u-form-item label-width="120" required="true" label="联系电话" prop="phone">
				<u-input v-model="form.phone" placeholder="请输入联系电话" />
			</u-form-item>
			<u-form-item label-width="120" required="true" label="项目地址" prop="project_address">
				<u-input v-model="form.project_address" placeholder="请选择项目地址" type="select" :select-open="addressShow" @click="addressShow = true" />
				<u-picker mode="region" v-model="addressShow" @confirm="addressConfirm"></u-picker>
			</u-form-item>
			<u-form-item label-width="120" required="true" label="开始日期" prop="start_date">
				<u-input v-model="form.start_date" placeholder="请选择开始日期" type="select" :select-open="timeShow1" @click="timeShow1 = true" />
				<u-picker mode="time" v-model="timeShow1" @confirm="timeConfirm1"></u-picker>
			</u-form-item>
			<u-form-item label-width="120" required="true" label="截止日期" prop="end_date">
				<u-input v-model="form.end_date" placeholder="请选择截止日期" type="select" :select-open="timeShow2" @click="timeShow2 = true" />
				<u-picker mode="time" v-model="timeShow2" @confirm="timeConfirm2"></u-picker>
			</u-form-item>
			<u-form-item label-width="120" required="true" label="其他要求" prop="others">
				<u-input v-model="form.others" type="textarea" height="100" :auto-height="true" />
			</u-form-item>
		</u-form>
		<u-button class='u-margin-20' :disabled="submitBtnDis" type="primary" @click="submit">确认添加</u-button>
		<!-- 顶部提示 -->
		<u-top-tips ref="uTips"></u-top-tips>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {verify} from "@/common/verify.js";
	import {mapState} from "vuex";
	export default {
		data() {
			return {
				timeShow1:false,
				timeShow2:false,
				timestamp1:0,
				timestamp2:0,
				addressShow:false,
				form: {
					project_name: '',
					start_date: "",
					end_date: "",
					phone:"",
					project_address: "",
					others:"",
					status:1
				},
				rules: {
					project_name: [{
						required: true,
						message: '请输入项目名称',
						trigger: ['blur', 'change']
					}],
					start_date: [{
						required: true,
						message: "请选择开始日期",
						tigger: ['blur', 'change']
					}],
					end_date: [{
						required: true,
						message: "请选择截止日期",
						trigger: ['blur', 'change']
					}],
					phone:[{
						required: true,
						message: "请输入电话号码",
						trigger: ['blur', 'change']
					}],
					project_address:[{
						required: true,
						message: "请请输入项目地址",
						trigger: ['blur', 'change']
					}]
				}
			}
		},
		// 设置校验规则
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		computed:{
			...mapState({
				token:"token",
				userID:"userID"
			})
		},
		methods: {
			timeConfirm1(e){
				this.timestamp1 = e.timestamp;
				this.form.start_date = `${e.year}-${e.month}-${e.day}`;
			},
			timeConfirm2(e){
				this.timestamp2 = e.timestamp;
				this.form.end_date = `${e.year}-${e.month}-${e.day}`;
			},
			addressConfirm(e){
				if(e.city.label == '市辖区'){
					this.form.project_address = `${e.province.label} ${e.area.label}`
				} else {
					this.form.project_address = `${e.province.label} ${e.city.label} ${e.area.label}`
				}
			},
			submit() {
				this.$refs.uForm.validate(async valid => {
					if (valid) {
						let {form,token,userID} = this;
						let isPhone = await verify.verifyPhone(parseInt(form.phone)),
							isTelephone = await verify.verifyTelephone(form.phone);
						if(isPhone && isTelephone){
							this.$refs.uTips.show({
								title: '电话格式错误',
								type: 'warning'
							})
							return false;
						}
						if(this.timestamp1 >= this.timestamp2){
							this.$refs.uTips.show({
								title: '请选择正确的起止时间',
								type: 'warning'
							})
							return false;
						}
						
						this.submitBtnDis = true;
						this.form.userID = userID;
						this.$loading("正在发布")
						let res = await this.$network({
							api:"main",
							data:{
								action:"recruitment/pmRecruitmentAdmin",
								data:{
									data:form,
									token,
									type:"add"
								}
							}
						});
						uni.hideLoading();
						this.submitBtnDis = false;
						if (res.status === 0) {
							Object.assign(this.$data,this.$options.data())
							this.$refs.uToast.show({
								title: '发布成功',
								type: 'success'
							})
						}
					}
				})
			}
		}
	}
</script>
